/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Order1;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Taxonomicelement;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class Order1JpaController {

    public Order1JpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    public Order1 save(Order1 order1) {
        Order1 aux = findOrder1(order1.getOrder());
        if(aux==null)
            return create(order1);
        else
            return aux;
    }

    public Order1 create(Order1 order1) {
        Order1 r = null;
        if (order1.getTaxonomicelementList() == null) {
            order1.setTaxonomicelementList(new ArrayList<Taxonomicelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Taxonomicelement> attachedTaxonomicelementList = new ArrayList<Taxonomicelement>();
            for (Taxonomicelement taxonomicelementListTaxonomicelementToAttach : order1.getTaxonomicelementList()) {
                taxonomicelementListTaxonomicelementToAttach = em.getReference(taxonomicelementListTaxonomicelementToAttach.getClass(), taxonomicelementListTaxonomicelementToAttach.getIdtaxonomicelement());
                attachedTaxonomicelementList.add(taxonomicelementListTaxonomicelementToAttach);
            }
            order1.setTaxonomicelementList(attachedTaxonomicelementList);
            r = em.merge(order1);
            for (Taxonomicelement taxonomicelementListTaxonomicelement : order1.getTaxonomicelementList()) {
                Order1 oldOrder1OfTaxonomicelementListTaxonomicelement = taxonomicelementListTaxonomicelement.getOrder1();
                taxonomicelementListTaxonomicelement.setOrder1(order1);
                taxonomicelementListTaxonomicelement = em.merge(taxonomicelementListTaxonomicelement);
                if (oldOrder1OfTaxonomicelementListTaxonomicelement != null) {
                    oldOrder1OfTaxonomicelementListTaxonomicelement.getTaxonomicelementList().remove(taxonomicelementListTaxonomicelement);
                    oldOrder1OfTaxonomicelementListTaxonomicelement = em.merge(oldOrder1OfTaxonomicelementListTaxonomicelement);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Order1 order1) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Order1 persistentOrder1 = em.find(Order1.class, order1.getIdorder());
            List<Taxonomicelement> taxonomicelementListOld = persistentOrder1.getTaxonomicelementList();
            List<Taxonomicelement> taxonomicelementListNew = order1.getTaxonomicelementList();
            List<Taxonomicelement> attachedTaxonomicelementListNew = new ArrayList<Taxonomicelement>();
            for (Taxonomicelement taxonomicelementListNewTaxonomicelementToAttach : taxonomicelementListNew) {
                taxonomicelementListNewTaxonomicelementToAttach = em.getReference(taxonomicelementListNewTaxonomicelementToAttach.getClass(), taxonomicelementListNewTaxonomicelementToAttach.getIdtaxonomicelement());
                attachedTaxonomicelementListNew.add(taxonomicelementListNewTaxonomicelementToAttach);
            }
            taxonomicelementListNew = attachedTaxonomicelementListNew;
            order1.setTaxonomicelementList(taxonomicelementListNew);
            order1 = em.merge(order1);
            for (Taxonomicelement taxonomicelementListOldTaxonomicelement : taxonomicelementListOld) {
                if (!taxonomicelementListNew.contains(taxonomicelementListOldTaxonomicelement)) {
                    taxonomicelementListOldTaxonomicelement.setOrder1(null);
                    taxonomicelementListOldTaxonomicelement = em.merge(taxonomicelementListOldTaxonomicelement);
                }
            }
            for (Taxonomicelement taxonomicelementListNewTaxonomicelement : taxonomicelementListNew) {
                if (!taxonomicelementListOld.contains(taxonomicelementListNewTaxonomicelement)) {
                    Order1 oldOrder1OfTaxonomicelementListNewTaxonomicelement = taxonomicelementListNewTaxonomicelement.getOrder1();
                    taxonomicelementListNewTaxonomicelement.setOrder1(order1);
                    taxonomicelementListNewTaxonomicelement = em.merge(taxonomicelementListNewTaxonomicelement);
                    if (oldOrder1OfTaxonomicelementListNewTaxonomicelement != null && !oldOrder1OfTaxonomicelementListNewTaxonomicelement.equals(order1)) {
                        oldOrder1OfTaxonomicelementListNewTaxonomicelement.getTaxonomicelementList().remove(taxonomicelementListNewTaxonomicelement);
                        oldOrder1OfTaxonomicelementListNewTaxonomicelement = em.merge(oldOrder1OfTaxonomicelementListNewTaxonomicelement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = order1.getIdorder();
                if (findOrder1(id) == null) {
                    throw new NonexistentEntityException("The order1 with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Order1 order1;
            try {
                order1 = em.getReference(Order1.class, id);
                order1.getIdorder();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The order1 with id " + id + " no longer exists.", enfe);
            }
            List<Taxonomicelement> taxonomicelementList = order1.getTaxonomicelementList();
            for (Taxonomicelement taxonomicelementListTaxonomicelement : taxonomicelementList) {
                taxonomicelementListTaxonomicelement.setOrder1(null);
                taxonomicelementListTaxonomicelement = em.merge(taxonomicelementListTaxonomicelement);
            }
            em.remove(order1);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Order1> findOrder1Entities() {
        return findOrder1Entities(true, -1, -1);
    }

    public List<Order1> findOrder1Entities(int maxResults, int firstResult) {
        return findOrder1Entities(false, maxResults, firstResult);
    }

    private List<Order1> findOrder1Entities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Order1.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    public Order1 findOrder1(String q) {
        Query query= getEntityManager().createNamedQuery("Order1.findByOrder1");
        query.setParameter("order1",q);

        return query.getResultList().isEmpty()?null:(Order1)query.getResultList().get(0);
    }
    public Order1 findOrder1(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Order1.class, id);
        } finally {
            em.close();
        }
    }

    public int getOrder1Count() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Order1> rt = cq.from(Order1.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
